--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    每一级提升2饱度，5生命，2精神，0.01体力系数a
    等级锁：7,14,21,28,35，42

    升级机制如下：
    0-10级范围内，每一级升级所需经验为21（X+1），X为胡桃当前等级。
    11-20级，每一级升级所需经验为21（X+8），X为胡桃当前等级。
    21-30级，每一级升级所需经验为21（X+15），X为胡桃当前等级。
    31-40级，每一级升级所需经验为21（X+22），X为胡桃当前等级。
    41-49级，每一级升级所需经验为21（X+29），X为胡桃当前等级。


]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local function any_unlocker_fn(inst)
        return true
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    if not TheWorld.ismastersim then
        return
    end
    inst:AddComponent("hutao_com_level_sys")
    -----------------------------------------------------------------
    --- 初始化参数
        inst.components.hutao_com_level_sys:SetLevelMax(49)
    -----------------------------------------------------------------
    --- 等级锁
        inst.components.hutao_com_level_sys:SetLevelUpLocker(7,any_unlocker_fn)
        inst.components.hutao_com_level_sys:SetLevelUpLocker(14,any_unlocker_fn)
        inst.components.hutao_com_level_sys:SetLevelUpLocker(21,any_unlocker_fn)
        inst.components.hutao_com_level_sys:SetLevelUpLocker(28,any_unlocker_fn)
        inst.components.hutao_com_level_sys:SetLevelUpLocker(35,any_unlocker_fn)
        inst.components.hutao_com_level_sys:SetLevelUpLocker(42,any_unlocker_fn)
    -----------------------------------------------------------------
    --- 经验更新函数
        local function GetNextLevelUpExp(current_level)
            local next_level_exp_cost = 10
            if current_level <= 10 then
                next_level_exp_cost = 21 * (current_level + 1)
            elseif current_level <= 20 then
                next_level_exp_cost = 21 * (current_level + 8)
            elseif current_level <= 30 then
                next_level_exp_cost = 21 * (current_level + 15)
            elseif current_level <= 40 then
                next_level_exp_cost = 21 * (current_level + 22)
            elseif current_level <= 49 then
                next_level_exp_cost = 21 * (current_level + 29)
            end
            print("升级到下一级所需经验：",next_level_exp_cost)
            return next_level_exp_cost
        end
        inst.components.hutao_com_level_sys:SetExpMaxRefreshFn(function()
            local current_level = inst.components.hutao_com_level_sys:GetLevel()
            local next_level_exp_cost = GetNextLevelUpExp(current_level)
            inst.components.hutao_com_level_sys:SetExpMax(next_level_exp_cost)
        end)
        --- 初始化升级经验。
        inst:DoTaskInTime(0,function()
            local current_level = inst.components.hutao_com_level_sys:GetLevel()
            local next_level_exp_cost = GetNextLevelUpExp(current_level)
            inst.components.hutao_com_level_sys:SetExpMax(next_level_exp_cost)
        end)
    -----------------------------------------------------------------
    --- 配置三维初始化函数
        inst.components.hutao_com_max_value_controller:SetOriginMaxHunger(TUNING[string.upper("hutao").."_HUNGER"])
        inst.components.hutao_com_max_value_controller:SetOriginMaxSanity(TUNING[string.upper("hutao").."_SANITY"])
        inst.components.hutao_com_max_value_controller:SetOriginMaxHealth(TUNING[string.upper("hutao").."_HEALTH"])
    -----------------------------------------------------------------
    --- 参数更新
        local temp_inst = CreateEntity()
        inst:ListenForEvent("onremove",function()
            temp_inst:Remove()
        end)
        local function max_hunger_sanity_health_update()
            -- 每一级提升2饱度，5生命，2精神，0.01体力系数a
            local current_level = inst.components.hutao_com_level_sys:GetLevel()
            --- hunger 
            inst.components.hutao_com_max_value_controller:AddTempExtraHunger(temp_inst, current_level * 2)
            --- sanity
            inst.components.hutao_com_max_value_controller:AddTempExtraSanity(temp_inst, current_level * 2)
            --- health
            inst.components.hutao_com_max_value_controller:AddTempExtraHealth(temp_inst, current_level * 5)
            --- 体力系数a
            inst.components.hutao_com_physical_power:AddModulus(temp_inst, current_level * 0.01)
        end
        inst.components.hutao_com_level_sys:SetLevelRefreshFn(max_hunger_sanity_health_update)
        inst.components.hutao_com_level_sys:AddOnLoadFn(max_hunger_sanity_health_update)
        
    -----------------------------------------------------------------
    --- 死亡惩罚
        inst:ListenForEvent("death",function(inst)
            if inst.components.hutao_com_level_sys:IsLevelUpLocking() then
                inst.components.hutao_com_level_sys:LevelDown()
            end
        end)
    -----------------------------------------------------------------
    --- 台词本
        --- 升级的时候
        inst:ListenForEvent("hutao_com_level_sys.level_changed",function(inst,_table)
            local old_level = _table and _table.old or 0
            local new_level = _table and _table.new or 0
            if new_level > old_level then
                inst.components.hutao_com_dialogue:Say("level_up",1.5)
            end
        end)
        --- 被锁经验的时候。每天说一次。
        local level_up_locking_flag = false
        inst:ListenForEvent("hutao_com_level_sys.level_up_locking",function(inst)
            if not level_up_locking_flag then
                inst.components.hutao_com_dialogue:Say("level_up_locking",2)
                level_up_locking_flag = true
            end
        end)
        inst:WatchWorldState("cycles",function()
            level_up_locking_flag = false
        end)
    -----------------------------------------------------------------

end